<?php
/**
 * User: wxl
 * Date: 2016/5/13
 * Time: 12:54
 * Intro:个人中心service类
 */

class UserCenterService extends BaseService
{

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 交易记录
     * @param $userId
     * @param $accountId
     * @return array
     */
    public function tradeRecord($userId,$accountId){
        $moduleList = D('Module')->select();
        $returnData = array();
        $allMonth = array();
        foreach($moduleList as $k => $v){
            //统计洗衣、充电保相关记录
            if(in_array($v['module_id'],array(1,5))) {
                $data = $this->statisticLog($accountId,$v['module_name'],$userId,5);
                if($data) {
                    foreach($data as $key => $value) {
                        if(!in_array(date('Y-m',$value['create_time']),$allMonth)) {
                            array_push($allMonth,date('Y-m',$value['create_time']));
                        }
                        $data[$key]['create_time_to_date'] = date('Y-m',$value['create_time']);
                        $data[$key]['module_id'] = $v['module_id']; //根据module_id和type值确定模块、区分充值还是消费


                        if($v['module_id']  == 1) {
                            $ratio = $this->getMoneyRatio(1,$accountId);
                            $data[$key]['payMoney'] = number_format($value['consume_time']/$ratio,2);

                            //删除消费金额少于0.01的记录
                            if(number_format($value['consume_time']/$ratio,2) < 0.01) {
                                unset($data[$key]);
                            }

                            if($value['type'] == 1) {
                                $data[$key]['title'] = '充电保消费';
                            }
                            if($value['type'] == 2) {
                                $data[$key]['title'] = '充电保充值';
                            }
                        } elseif($v['module_id']  == 5) {
                            $ratio = $this->getMoneyRatio(5,$accountId);
                            $data[$key]['payMoney'] = number_format($value['consume_time']/$ratio,2);

                            //删除消费金额少于0.01的记录
                            if(number_format($value['consume_time']/$ratio,2) < 0.01) {
                                unset($data[$key]);
                            }

                            if($value['type'] == 1) {
                                $data[$key]['title'] = '智能洗衣消费';
                            }
                            if($value['type'] == 2) {
                                $data[$key]['title'] = '智能洗衣充值';
                            }
                        }

                    }
                }
                $allData[$v['module_id']] = $data;
            } elseif(in_array($v['module_id'],array(4,6,7,8,9,10,12,13))) { //4表示电控系统、滨江后勤6用的是寝室充电模块
                $data = $this->statisticOrder($v['module_id'],$v['module_name'],$userId,5);
                if($data) {
                    foreach($data as $key => $value) {
                        if(!in_array(date('Y-m',$value['create_time']),$allMonth)) {
                            array_push($allMonth,date('Y-m',$value['create_time']));
                        }
                        $data[$key]['create_time_to_date'] = date('Y-m',$value['create_time']);
                        $data[$key]['module_id'] = $v['module_id']; //根据module_id确定模块
                        if($v['module_id'] == 7) {
                            $data[$key]['title'] = '微商城购物';
                        } elseif($v['module_id'] == 8) {
                            $data[$key]['title'] = '智能订水';
                        } elseif($v['module_id'] == 9) {
                            $data[$key]['title'] = '水费缴费';
                        } elseif($v['module_id'] == 10) {
                            $data[$key]['title'] = '智能订餐';
                        } elseif($v['module_id'] == 12) {
                            $data[$key]['title'] = '假日包车';
                        } elseif($v['module_id'] == 13) {
                            $data[$key]['title'] = '移动预充值';
                        } elseif($v['module_id'] == 4) {
                            $data[$key]['title'] = '电控系统';
                        } elseif($v['module_id'] == 6) {
                            $data[$key]['title'] = '寝室充电';
                        }

                    }
                }
                $allData[$v['module_id']] = $data;
            }
        }

        $i= 0 ;
        rsort($allMonth);
        foreach($allMonth as $item) {
            foreach($allData as $key => $value) {
                foreach($value as $k => $v) {
                    if($v['create_time_to_date'] == $item) {
                        $returnData[$item][$i++] = $v;
                    }
                }
            }
            $i = 0;
        }

        return $returnData;
    }

    /**
     * 获取时间对应的金额
     * @param $type
     * @param $accountId
     * @return mixed
     */
    public function getMoneyRatio($type,$accountId) {
        $condition = array(
            'account_id' => $accountId
        );
        if($type == 1) {
            $module = M('Charging');
        } elseif($type == 5) {
            $module = M('Laundry');
        }
        $ratio = $module->where($condition)->getField('ratio');
        return $ratio;
    }

    /**
     * 统计微商城、校园订水等相关订单数据
     * @param $moduleId
     * @param $module
     * @param $userId
     * @param $duration
     * @return mixed
     */
    public function statisticOrder($moduleId,$module,$userId,$duration) {

        if($moduleId != 13) {
            $mod = M($module."Order");
        }
        if($moduleId && $module && $userId) {
            switch($moduleId) {
                case 7: //微商城
                    $condition = array(
                        'status' => 4, //已完成
                        'user_id' => $userId,
                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                    );
                    $field = 'order_amount as payMoney,pay_time,create_time';
                    break;
                case 8: //智能订水
                    $condition = array(
                        'status' => array('in',array(3,4,5)), //派送完成
                        'user_id' => $userId,
                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                        );
                    $field = 'goods_amount as payMoney,pay_time,create_time';
                    break;
                case 9: //水费缴费
                    $condition = array(
                        'status' => 1, //已支付
                        'user_id' => $userId,
                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                    );
                    $field = 'goods_amount as payMoney,pay_time,create_time';
                    break;
                case 10: //智能订餐
                    $condition = array(
                        'cut_off' => 0,
                        'status' => array('in','3,4'), //已确定收货、已评价
                        'user_id' => $userId,
                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                    );
                    $field = 'goods_amount as payMoney,pay_time,create_time';
                    break;
                case 12: //假日包车
                    $condition = array(
                        'status' => 1, //已付款
                        'user_id' => $userId,
                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                    );
                    $field = 'goods_amount as payMoney,pay_time,create_time';
                    break;
                case 13: //假日包车
                    $condition = array(
                        'status' => 3, //通过
                        'uid' => $userId,
                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                    );
                    $field = 'goods_amount as payMoney,pay_time,create_time';
                    $mod = M('SmsflowOrder');
                    break;
                case 4: //电控系统
                    $condition = array(
                        'status' => 3, //通过
                        'user_id' => $userId,
                        'origin_id' => array('neq',1), //过滤后台充值
                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                    );
                    $field = 'goods_amount as payMoney,pay_time,create_time';
                    $mod = M('EcsOrder');
                    break;
                case 6: //寝室充电
                    $condition = array(
                        'status' => array('in',array(1,3)), //通过
                        'user_id' => $userId,

                        'create_time' => array('egt',strtotime('-'.$duration.' month')),
                    );
                    $field = 'goods_amount as payMoney,pay_time,create_time';
                    $mod = M('ElectricityOrder');
                    break;

            }


            $data = $mod->where($condition)->field($field)->select();
            //echo $mod->getLastSql();
            if($data) {
                return $data;
            }

        }
    }

    /**
     * 统计洗衣、充电保相关记录
     * @param $module
     * @param $userId
     * @param int $duration
     * @return array|mixed
     */
    public function statisticLog($accountId,$module,$userId,$duration = 1) {

        if($module) {
            $mod = D($module."Log");
            //充值记录
            $condition = array(
                'type' => 2, //充值
                'status' => 1,
                'user_id' => $userId,
                'create_time' => array('egt',strtotime('-'.$duration.' month')),
            );

            //$condition[strtolower($module).'_id'] = M($module)->where(array('account_id' => $accountId))->getField(strtolower($module).'_id');
            $dataRecharge = $mod->where($condition)->field('type,consume_time,create_time')->select();
            //echo $mod->getLastSql();

            //消费记录
            $condition = array(
                'type' => 1, //消费
                'status' => 1,
                'user_id' => $userId,
                'create_time' => array('egt',strtotime('-'.$duration.' month')),
            );
            $dataConsume = $mod->where($condition)->field('website_id,type,consume_time,create_time')->select();
            //判断消费网点是否免费
            foreach($dataConsume as $key => $value) {
                $fee = M($module.'Website')->where(array('website_id' => $value['website_id']))->getField('fees');
                if($fee) {
                    unset($dataConsume[$key]);
                }
            }


            if($dataConsume && $dataRecharge) {
                $data = array_merge($dataRecharge,$dataConsume);
            }
            if(!$dataConsume && $dataRecharge) {
                $data = $dataRecharge;
            }
            if($dataConsume && !$dataRecharge) {
                $data = $dataConsume;
            }

            return $data;
        }
    }

    //获得地址列表
    public function getAddress($userId,$accountId){
        if($userId) {
            $condition = array(
                'user_id' => $userId,
                'cut_off' => 0,
                'account_id' => $accountId,
            );
            $mAddress = D('UserAddress');
            return $mAddress->getList($condition);
        }
    }

    /**
     * 获取当前用户特定时间内在各个平台的消费总额
     * @param $userId
     * @param $accountId
     * @param int $duration
     * @return array
     */
    public function getConsumeMoney($userId,$accountId,$duration = 1) {

        $moduleList = D('Module')->select();
        $returnData = array();
        $allMonth = array();
        foreach($moduleList as $k => $v){
            //统计洗衣、充电保、寝室充电相关记录
            if(in_array($v['module_id'],array(1,5))) {
                $data = $this->statisticLog($accountId,$v['module_name'],$userId,$duration);
                if($data) {
                    foreach($data as $key => $value) {
                        if(!in_array(date('Y-m',$value['create_time']),$allMonth)) {
                            array_push($allMonth,date('Y-m',$value['create_time']));
                        }
                        $data[$key]['create_time_to_date'] = date('Y-m',$value['create_time']);
                        $data[$key]['module_id'] = $v['module_id']; //根据module_id和type值确定模块、区分充值还是消费

                        if($v['module_id']  == 1) {
                            $ratio = $this->getMoneyRatio(1,$accountId);
                            $data[$key]['payMoney'] = number_format($value['consume_time']/$ratio,2);
                            if($value['type'] == 1) {
                                $data[$key]['title'] = '充电保消费';
                            }
                            if($value['type'] == 2) {
                                $data[$key]['title'] = '充电保充值';
                            }
                        } elseif($v['module_id']  == 5) {
                            $ratio = $this->getMoneyRatio(5,$accountId);
                            $data[$key]['payMoney'] = number_format($value['consume_time']/$ratio,2);
                            if($value['type'] == 1) {
                                $data[$key]['title'] = '智能洗衣消费';
                            }
                            if($value['type'] == 2) {
                                $data[$key]['title'] = '智能洗衣充值';
                            }
                        }

                    }
                }
                $allData[$v['module_id']] = $data;
            } elseif(in_array($v['module_id'],array(4,6,7,8,9,10,12,13))) {
                $data = $this->statisticOrder($v['module_id'],$v['module_name'],$userId,$duration);
                if($data) {
                    foreach($data as $key => $value) {
                        if(!in_array(date('Y-m',$value['create_time']),$allMonth)) {
                            array_push($allMonth,date('Y-m',$value['create_time']));
                        }
                        $data[$key]['create_time_to_date'] = date('Y-m',$value['create_time']);
                        $data[$key]['module_id'] = $v['module_id']; //根据module_id确定模块
                        if($v['module_id'] == 7) {
                            $data[$key]['title'] = '微商城购物';
                        } elseif($v['module_id'] == 8) {
                            $data[$key]['title'] = '智能订水';
                        } elseif($v['module_id'] == 9) {
                            $data[$key]['title'] = '水费缴费';
                        } elseif($v['module_id'] == 10) {
                            $data[$key]['title'] = '智能订餐';
                        } elseif($v['module_id'] == 12) {
                            $data[$key]['title'] = '假日包车';
                        } elseif($v['module_id'] == 13) {
                            $data[$key]['title'] = '移动预充值';
                        } elseif($v['module_id'] == 4) {
                            $data[$key]['title'] = '电控系统';
                        } elseif($v['module_id'] == 6) {
                            $data[$key]['title'] = '寝室充电';
                        }

                    }
                }
                $allData[$v['module_id']] = $data;
            }

        }

        $i= 0 ;
        foreach($allMonth as $item) {
            foreach($allData as $key => $value) {
                foreach($value as $k => $v) {
                    if($v['create_time_to_date'] == $item) {
                        $returnData[$item][$i++] = $v;
                    }
                }
            }
            $i = 0;
        }

        return $returnData;
    }


    //获得个人中心客服中心信息
    public function customerCenter($accountId){
        $mAbout = D('UcenterAboutus');
        $list = $mAbout -> getList(array('account_id'=>$accountId,'cut_off'=>0),'title,content','sort asc');
        //图标
        $logo = D("Wechat")->where(array('account_id'=>$accountId))->field('logo,wechat_name')->find();
        return array($list,$logo);
    }
    
  
}